/**
 * file
 */
package com.tdkj.action.front.member.freeReleaseManagement;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.tdkj.action.BaseAction;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.selfadvert.BeanSelfAdvert;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.selfadvert.IBeanSelfAdvertService;

/**

 * FileName:     SelfAdvertisingAction.java

 * @Description: TODO自助广告

 * All rights Reserved, Designed By ZTE-ITS

 * Copyright:    Copyright(C) 2010-2011

 * Company       天成科技有限公司

 * @author:    李兴建

 * @version    V1.0 

 * Createdate:         2012-5-27 下午01:54:09

 *

 * Modification  History:

 * Date         Author        Version        Discription

 * -----------------------------------------------------------------------------------

 * 2012-5-27       李兴建          1.0             1.0

 * Why & What is modified: <修改原因描述>

 */
public class MySelfAdvertAction extends BaseAction {
	private Long id;
	private String type;
	private String[] ids;
	private BeanSelfAdvert advert;
	private IBeanSelfAdvertService beanSelfAdvertService;
	public String[] getIds() {
		return ids;
	}
	public void setIds(String[] ids) {
		this.ids = ids;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public BeanSelfAdvert getAdvert() {
		return advert;
	}
	public void setAdvert(BeanSelfAdvert advert) {
		this.advert = advert;
	}
	public IBeanSelfAdvertService getBeanSelfAdvertService() {
		return beanSelfAdvertService;
	}
	public void setBeanSelfAdvertService(
			IBeanSelfAdvertService beanSelfAdvertService) {
		this.beanSelfAdvertService = beanSelfAdvertService;
	}
	public String findList(){
		List<BeanSelfAdvert> s=beanSelfAdvertService.findTextList();
		if(s.size()==0){appendObj(20,"1");appendObj(20,"2");}
		Criteria criteria = this.createCriteria(BeanSelfAdvert.class);
		//加限制条件，只处理用户相关
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "mySelfAdvertAction!findList.action", 10);
		return "list";
	}
	public String findEditPage(){
		advert=beanSelfAdvertService.findById(this.getId(), BeanSelfAdvert.class);
		return "editPage";
	}
	public String findAddPage(){
		return "addPage";
	}
	public String editSelfAdvert(){
		beanSelfAdvertService.saveOrUpdate(advert, BeanSelfAdvert.class);
		return findList();
	}
	public String deleteSelfAdvert(){
		beanSelfAdvertService.delete(this.getId(), BeanSelfAdvert.class);
		return findList();
	}
	public String addSelfAdvert(){
		BeanSelfAdvert bean=new BeanSelfAdvert();
		bean=this.getAdvert();
		bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
		String type=bean.getAdverttype();
		beanSelfAdvertService.save(bean);
		return findList();
	}
	private  void appendObj(int num,String type) {
		// TODO Auto-generated method stub
		BeanSelfAdvert bean=null;
		for(int i=0;i<num;i++){
			bean=new BeanSelfAdvert();
			bean.setEnterpricename("中华科技有限公司");
			bean.setEmail("zk@345.com");
			bean.setLinkman("张封魔");
			bean.setTitle("好东西"+i);
			bean.setContent("好东西的内容展示"+i);
			bean.setFax("123-098765");
			BeanUser u=beanSelfAdvertService.findById(5, BeanUser.class);
			bean.setUser(u);
			bean.setStatus("未审核");
			if(type.equals("1")){
				bean.setAdverttype("1");
			}
			if(type.equals("2")){bean.setAdverttype("2");bean.setImgurl("images/"+(i+1)+".png");}
			bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
			beanSelfAdvertService.save(bean);
		}
	}
	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanSelfAdvert.class);
			}
		}
		return findList();
	}

}
